<template>
  <div class="generator-wrapper">
    <el-table :data="dataSource" border v-loading="loading" style="width: 100%">
      <el-table-column
        v-for="(column, index) in displayFieldList"
        :key="column.prop"
        :width="column.width"
        :fixed="
          column.key === 'action' || column.dataIndex === 'action'
            ? 'right'
            : false
        "
      >
        <template #header>
          <div @click="handleHeaderCellCLick(index)" style="cursor: pointer">
            {{
              column.key === "action" || column.dataIndex === "action"
                ? "操作"
                : column.title
            }}
          </div>
        </template>
        <template #default="scope">
          <template
            v-if="column.key === 'action' || column.dataIndex === 'action'"
          >
            <a> 编辑 </a>
            <a> 详情 </a>
            <a> 删除 </a>
          </template>
          <template v-else-if="column.title === '序号'">
            <span>
              {{ scope.$index + 1 }}
            </span>
          </template>
          <template v-else>
            <span>
              {{ scope.row[column.dataIndex] }}
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end">
      <el-pagination
        style="text-align: right; margin-top: 20px"
        v-bind="pagination"
        v-model:current-page="pagination.currentPage"
        :page-size="10"
        :total="total"
        layout="prev, pager, next"
        @current-change="handlePageChange"
        @prev-click="handlePageChange"
        @next-click="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from "vue";
import { storeToRefs } from "pinia";
import { useTableStore } from "@/store/table";

const store = useTableStore();
const { tableProps, fieldList } = storeToRefs(store);

const loading = ref(false);
const dataSource = ref([]);

const displayFieldList = computed(() => {
  return fieldList.value
    .filter((column) => !column.hiddenInTable)
    .map((column, index) => {
      return {
        ...column,
      };
    });
});

function handleHeaderCellCLick(index) {
  store.setCurrentField(fieldList.value[index]);
}

// TODO:选择未实现
const rowSelectionOption = ref({
  selectedRowKeys: [],
  selectRows: [],
  onChange: (selectedRowKeys, selectedRows) => {
    rowSelectionOption.value.selectedRowKeys = selectedRowKeys;
    rowSelectionOption.value.selectedRows = selectedRows;
  },
});

const rowSelection = computed(() =>
  tableProps.value.rowSelectionType
    ? {
        type: tableProps.value.rowSelectionType,
        ...rowSelectionOption.value,
      }
    : null
);

const pagination = ref({
  background: false,
  currentPage: 1,
});
const total = ref(0);
function handlePageChange(val) {
  fetchData({}, val);
}
async function fetchData(searchForm = {}, page = 1) {
  if (fieldList.value.length > 0) {
    loading.value = true;
    const response = await fetch("/mock/list", {
      method: "POST",
      body: JSON.stringify({
        searchForm,
        fieldList: fieldList.value,
      }),
    });
    const res = await response.json();
    setTimeout(() => {
      loading.value = false;
      dataSource.value = res.data.records.slice(0, 10);
      total.value = res.data.total;
      pagination.value.currentPage = page;
      console.log("🚀 ~ fetchData ~ es.data.records:", res.data);
    }, 300);
  }
}

function handleQueryList(searchForm) {
  fetchData(searchForm, 1);
}

defineExpose({
  handleQueryList,
});

watch(
  () => fieldList.value,
  () => {
    handleQueryList({});
  }
);

onMounted(() => {
  handleQueryList({});
});
</script>

<style lang="less" scoped>
:deep(.ant-table-thead .ant-table-cell) {
  cursor: pointer;
}
.generator-wrapper {
  box-sizing: border-box;
  padding: 15px;
  background: #fff;
  height: 100%;
}
</style>
